package Arrays_sort1;

import java.util.Arrays;
import java.util.Comparator;

public class Test {
    public static void main(String[] args) {
        Student[] students = new Student[4];
        students[0] = new Student("张三", 180, 18);
        students[1] = new Student("李四", 170, 20);
        students[2] = new Student("王五", 160, 22);
        students[3] = new Student("赵六", 150, 17);
        System.out.println("排序前：" + Arrays.toString(students));


        //推荐使用匿名内部类定义一个排序的方法
        Arrays.sort(students, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o2.getAge() - o1.getAge();
            }
        });
        //lomdba简化后
        //Arrays.sort(students, ( o1,  o2) -> o2.getAge() - o1.getAge());
        //打印结果
        for (int i = 0; i < students.length; i++) {
            System.out.println(students[i]);
        }


    }
}
